package com.ruoyi.merchant.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.merchant.domain.MerchantApplication2;
import com.ruoyi.merchant.domain.dto.MerchantApplicationRequest;
import com.ruoyi.merchant.domain.vo.MerchantApplicationResponse;

import com.ruoyi.merchant.service.IMerchantApplication2Service;
import com.ruoyi.merchant.utils.MerchantSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家入驻申请Controller
 */
@RestController
@RequestMapping("/merchant/application")
public class MerchantApplication2Controller extends BaseController {

    @Autowired
    private IMerchantApplication2Service applicationService2;

    /**
     * 提交入驻申请
     */
    @PostMapping("/submit")
    public AjaxResult submit(@Validated @RequestBody MerchantApplicationRequest request) {
        // 从token中获取当前登录商家ID
        Long merchantId = MerchantSecurityUtils.getMerchantId();
        
        MerchantApplicationResponse response = 
            applicationService2.submitApplication(request, merchantId);
        
        return AjaxResult.success("提交成功，请等待审核", response);
    }

    /**
     * 查询申请历史记录
     */
    @GetMapping("/history")
    public AjaxResult getHistory() {
        Long merchantId = MerchantSecurityUtils.getMerchantId();
        List<MerchantApplication2> history =
            applicationService2.getApplicationHistory(merchantId);
        return AjaxResult.success(history);
    }

    /**
     * 获取最新地申请记录
     */
    @GetMapping("/latest")
    public AjaxResult getLatest() {
        Long merchantId = MerchantSecurityUtils.getMerchantId();
        MerchantApplication2 application =
            applicationService2.getLatestApplication(merchantId);
        return AjaxResult.success(application);
    }
}
